﻿using System.Linq.Expressions;
using Devonline.AspNetCore;
using Devonline.Entity;

namespace Devonline.Database.NoSQL;

/// <summary>
/// NoSQL 数据库服务
/// </summary>
public interface INoSQLDataService
{
    /// <summary>
    /// 写入一行记录到数据库
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <param name="model">要写入的数据</param>
    /// <returns></returns>
    Task AddAsync<TModel>(TModel model) where TModel : class, new();
    /// <summary>
    /// 写入多行记录到数据库
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <param name="data">要写入的数据</param>
    /// <returns></returns>
    Task AddsAsync<TModel>(IEnumerable<TModel> data) where TModel : class, new();

    /// <summary>
    /// 更新一行记录到数据库
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <param name="model">要更新的数据</param>
    /// <returns></returns>
    Task UpdateAsync<TModel>(TModel model) where TModel : class, IEntitySet, new();
    /// <summary>
    /// 更新多行记录到数据库
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <param name="data">要更新的数据</param>
    /// <returns></returns>
    Task UpdatesAsync<TModel>(IEnumerable<TModel> data) where TModel : class, IEntitySet, new();

    /// <summary>
    /// 删除记录
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <typeparam name="TKey">业务数据主键的类型</typeparam>
    /// <param name="id">业务数据主键</param>
    /// <param name="context">数据操作上下文</param>
    /// <returns></returns>
    Task DeleteAsync<TModel, TKey>(TKey id, DataServiceContext? context = default) where TModel : class, IEntitySet;
    /// <summary>
    /// 删除记录
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <typeparam name="TKey">业务数据主键的类型</typeparam>
    /// <param name="ids">业务数据主键集合</param>
    /// <param name="context">数据操作上下文</param>
    /// <returns></returns>
    Task DeletesAsync<TModel, TKey>(IEnumerable<TKey> ids, DataServiceContext? context = default) where TModel : class, IEntitySet;

    /// <summary>
    /// 根据过滤条件返回查询结果
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <returns></returns>
    Task<IQueryable<TModel>> GetQueryableAsync<TModel>() where TModel : class, new();
    /// <summary>
    /// 根据过滤条件返回查询结果
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <param name="predicate">查询表达式</param>
    /// <returns></returns>
    Task<IQueryable<TModel>> GetQueryableAsync<TModel>(Expression<Func<TModel, bool>> predicate) where TModel : class, new();
    /// <summary>
    /// 根据条件查询第一条记录
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <param name="predicate">查询表达式</param>
    /// <returns></returns>
    Task<TModel?> FirstOrDefaultAsync<TModel>(Expression<Func<TModel, bool>> predicate) where TModel : class, new();
    /// <summary>
    /// 从数据库查询记录
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <returns>查询结果</returns>
    Task<PagedResult<TModel>> GetPagedResultAsync<TModel>() where TModel : class, new();
    /// <summary>
    /// 从数据库查询记录
    /// </summary>
    /// <typeparam name="TModel">数据对象模型的类型</typeparam>
    /// <param name="predicate">查询表达式</param>
    /// <returns>查询结果</returns>
    Task<PagedResult<TModel>> GetPagedResultAsync<TModel>(Expression<Func<TModel, bool>> predicate) where TModel : class, new();
}